﻿Public Class frmLogin
    Private role As String

    Public Property UserRole As String
        Get
            Return role
        End Get
        Set(ByVal value As String)
            role = value
        End Set
    End Property

    Public ReadOnly Property RoleAdmin As String
        Get
            Return "admin"
        End Get
    End Property

    Public ReadOnly Property RoleUser As String
        Get
            Return "user"
        End Get
    End Property

    Private userDa As New DSVaultingTableAdapters.user_acountTableAdapter

    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

        Dim username As String = UsernameTextBox.Text
        Dim password As String = PasswordTextBox.Text

        ' Login validation
        If (username = "" Or password = "") Then
            MsgBox("សូមបំពេញ ឈ្មោះ និងពាក្យសំងាត់អោយបានត្រឹមត្រូវ", MsgBoxStyle.Exclamation, "សារប្រាប់កំហុស")
        ElseIf CheckUserPass(username, password) Then
            Main.Hide()
            Me.Hide()
            frmMonthYearPicker.Show()
        Else
            MsgBox("សូមអភ័យទោស ឈ្មោះគណនី ឬពាក្យសំងាត់មិនត្រឹមត្រូវទេ។", MsgBoxStyle.Critical, "សារប្រាប់កំហុស")
        End If
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    ' Check username againt password in the dataset
    ' Return result with true or false if the user and password correct or not

    Private Function CheckUserPass(ByVal UserName As String, ByVal Password As String) As Boolean
        Dim dt As DataTable
        Dim count As Integer = userDa.GetDataByUserPass(UserName, Password).Count
        If count > 0 Then
            dt = userDa.GetDataByUserPass(UserName, Password)
            Me.UserRole = dt.Rows(0).Item("ROLES")
            Return True
        Else
            Return False
        End If
    End Function

End Class
